package com.yanqu.road.server.cmd.activity.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityRankMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ORE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, desc = "获取商会成员贡献值")
public class OreWarActivityMemberConditionDataCmd extends AbstractCommand {

	private long serverId;

	private ActivityInfo activityInfo;

	private int activityId;

	private String unionUid;

	private int type;

	private UnionActivityConditionData unionActivityConditionData;

	private boolean sendResp = false;

	private OreWarActivityProto.OreWarActivityConditionUnionMemberDataReqMsg reqMsg;


	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = OreWarActivityProto.OreWarActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		unionUid = reqMsg.getUnionUid();
		type = reqMsg.getType();
		serverId = reqMsg.getServerId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ORE_ACTIVITY_NO_IN_TIME;
			return;
		}
		if(type != OreWarActivityMgr.CONDITION_TYPE_UNION){
			ret = GameErrorCode.E_ORE_ACTIVITY_TYPE_NO_UNION_RANK;
			return;
		}
		if(false){
			sendResp = true;
			unionActivityConditionData = OreWarActivityRankMgr.getUnionActivityConditionData(activityId, unionUid);
			if(null == unionActivityConditionData){
				ret = GameErrorCode.E_ORE_ACTIVITY_NO_JOIN;
				return;
			}
		}else {
			/*YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_OREWAR_MEMBER_CONDITION_DATA, reqMsg);
			GamePlayerMgr.sendChannelPacket(player.getUserId(), pbMsg);*/
//			CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.Builder msg = CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.newBuilder();
//			msg.setActivityId(activityId);
//			msg.setUnionUid(unionUid);
//			msg.setServerId(serverId);
//			msg.setType(type);
//			msg.setRankType(3);
//			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_MEMBER_VALUE, msg);
//			GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
			YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
			builder.setActivityId(activityInfo.getActivityId());
			builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
			builder.setIdKey(String.valueOf(OreWarActivityMgr.CONDITION_TYPE_UNION));
			builder.setSeeUnionUid(unionUid);
			player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_CONTRIBUTION, builder, activityId);
		}

	}

	@Override
	public void sendData(GamePlayer player) {
		if(sendResp){
			OreWarActivityProto.OreWarActivityConditionUnionMemberDataRespMsg.Builder respMsg = OreWarActivityProto.OreWarActivityConditionUnionMemberDataRespMsg.newBuilder();
			respMsg.setRet(ret);
			if(0 == ret){
				if(null != unionActivityConditionData) {
					ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityPb.parseActivityConditionUnionMemberDataListMsg(
							UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, OreWarActivityRankMgr.getUserActivityConditionDataMap(activityId)));
					respMsg.setDataList(dataMsg);
				}
			}
			player.sendPacket(Protocol.U_ORE_WAR_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
		}
	}

}
